package com.cssy.xxkj.logic.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.dao.LogicExcelToStoreDAO;
import com.cssy.xxkj.logic.entity.LogicExcelToStoreDO;
import com.cssy.xxkj.logic.service.ILogicExcelToStoreService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.IDCodeTypeUtil;
@Service("logicExcelToStoreService")
public class LogicExcelToStoreServiceImpl implements ILogicExcelToStoreService {

	
	@Resource(name = "logicExcelToStoreDAO") 
	private LogicExcelToStoreDAO logicExcelToStoreDAO;
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult sr = new SearchResult(); 
		sr.setResults(logicExcelToStoreDAO.find(scs));
		sr.setRecordCount(logicExcelToStoreDAO.count(scs));
		return sr;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(LogicExcelToStoreDO t, User user)
			throws SyBaseException {
		if(t.getBizModifyDate()==null){
			t.setBizModifyDate(new Date());
		}
		t.setBizModifyName(user.getName());
		t.setBizVersion(t.getBizVersion()+1);
		return logicExcelToStoreDAO.updateLogicExcelToStoreDO(t);
	}

	@Override
	public LogicExcelToStoreDO save(LogicExcelToStoreDO t, User user)
			throws SyBaseException {
		if(t.getBizCreatorDate()==null){
			t.setBizCreatorDate(new Date());
		}
		t.setBizCreatorName(user.getName());
		t.setBizVersion(1);
		t.setBizModifyDate(new Date());
		t.setBizModifyName(user.getName());
		t.setBizVersionMark(1);
		logicExcelToStoreDAO.insertLogicExcelToStoreDO(t);
		return t;
	}

	@Override
	public LogicExcelToStoreDO findListById(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public LogicExcelToStoreDO saveLogicExcelToStoreDO(LogicExcelToStoreDO t,
			User user) throws SyBaseException {
		if(null==t.getId()||t==null){
			t.setId(IDCodeTypeUtil.getId());
			return save(t, user);
		}else{
			LogicExcelToStoreDO ld=logicExcelToStoreDAO.findLogicExcelToStoreDOByPrimaryKey(t.getId());
			if(ld==null){
				return save(t, user);
			}else{
				SimpleDateFormat sdf= new SimpleDateFormat("MM");
				String month=sdf.format(new Date());
				if(month.equals(sdf.format(ld.getBizCreatorDate()))){
					update(t, user);
					return t;
				}
				t.setId(IDCodeTypeUtil.getId());
				return save(t, user);
			}
		}
	}

	@Override
	public void deleteLogicExcelToStoreByMonth(SearchConditions scs)
			throws SyBaseException {
		this.logicExcelToStoreDAO.deleteLogicExcelToStoreByMonth(scs);
	}

	
}
